/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.entities.Historique;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Poste;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class HistoriqueJpaController implements Serializable {

    public HistoriqueJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Historique historique) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Poste nomPoste = historique.getNomPoste();
            if (nomPoste != null) {
                nomPoste = em.getReference(nomPoste.getClass(), nomPoste.getNomPoste());
                historique.setNomPoste(nomPoste);
            }
            em.persist(historique);
            if (nomPoste != null) {
                nomPoste.getHistoriqueCollection().add(historique);
                nomPoste = em.merge(nomPoste);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Historique historique) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Historique persistentHistorique = em.find(Historique.class, historique.getHistoId());
            Poste nomPosteOld = persistentHistorique.getNomPoste();
            Poste nomPosteNew = historique.getNomPoste();
            if (nomPosteNew != null) {
                nomPosteNew = em.getReference(nomPosteNew.getClass(), nomPosteNew.getNomPoste());
                historique.setNomPoste(nomPosteNew);
            }
            historique = em.merge(historique);
            if (nomPosteOld != null && !nomPosteOld.equals(nomPosteNew)) {
                nomPosteOld.getHistoriqueCollection().remove(historique);
                nomPosteOld = em.merge(nomPosteOld);
            }
            if (nomPosteNew != null && !nomPosteNew.equals(nomPosteOld)) {
                nomPosteNew.getHistoriqueCollection().add(historique);
                nomPosteNew = em.merge(nomPosteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = historique.getHistoId();
                if (findHistorique(id) == null) {
                    throw new NonexistentEntityException("The historique with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Historique historique;
            try {
                historique = em.getReference(Historique.class, id);
                historique.getHistoId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historique with id " + id + " no longer exists.", enfe);
            }
            Poste nomPoste = historique.getNomPoste();
            if (nomPoste != null) {
                nomPoste.getHistoriqueCollection().remove(historique);
                nomPoste = em.merge(nomPoste);
            }
            em.remove(historique);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Historique> findHistoriqueEntities() {
        return findHistoriqueEntities(true, -1, -1);
    }

    public List<Historique> findHistoriqueEntities(int maxResults, int firstResult) {
        return findHistoriqueEntities(false, maxResults, firstResult);
    }

    private List<Historique> findHistoriqueEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Historique.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Historique findHistorique(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Historique.class, id);
        } finally {
            em.close();
        }
    }

    public int getHistoriqueCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Historique> rt = cq.from(Historique.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
